/**  
 * @Title: BaiduAPI.java
 * @Package org.csun.nc.utils.map
 * @Description: TODO
 * @author chisj chisj@foxmail.com
 * @date 2018年9月3日
 */
package com.csun.cmny.provider.utils.map;

import org.apache.log4j.Logger;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ClassName: BaiduAPI 
 * @Description: TODO
 * @author chisj chisj@foxmail.com
 * @date 2018年9月3日
 */
public class BaiduAPI {

	private final static String ak = "oEMUk2R653OdTdUhW6PgSrO0CGOOYG4N";
	
	// 记录日志
	static Logger logger = Logger.getLogger(BaiduAPI.class);

	/**
	* @Title: getDistanceByPoints
	* @Description: 调用百度API接口计算两点驾车距离
	* @return 返回map
	*/
	@SuppressWarnings("unchecked")
	public static Map<String, Object> getDistanceByPoints(String startPoint,String endPoint) {

	//存放返回值map
	Map<String, Object> map = new HashMap<String, Object>();


	//Map<String,Object> startPointMap = JsonUtil.getMap4Json(startPoint);
	//Map<String,Object> endPointMap = JsonUtil.getMap4Json(endPoint);
	
	Map<String, Object> startPointMap = new HashMap<String, Object>();
	startPointMap.put("lng", "114.4291917726292");
	startPointMap.put("lat", "30.418032456341848");
	
	Map<String, Object> endPointMap = new HashMap<String, Object>();
	endPointMap.put("lng", "114.42473839917815");
	endPointMap.put("lat", "30.420710098164225");
	

	//拼接计算两点驾车距离的百度API接口
	String url =  String.format("http://api.map.baidu.com/routematrix/v2/driving?"+
	"output=json&origins=" + Double.parseDouble(StringUtil.convertMapString(startPointMap, "lat"))+
	"," + Double.parseDouble(StringUtil.convertMapString(startPointMap, "lng"))+
	"&destinations="+Double.parseDouble(StringUtil.convertMapString(endPointMap, "lat"))+
	"," + Double.parseDouble(StringUtil.convertMapString(endPointMap, "lng"))+
	"&ak=" + ak);

	String json = loadJSON(url);  
	 
	//将返回结果转换成map
	Map<String, Object> reMap = JsonUtil.getMap4Json(json);

	      logger.info("请求数据:  "+reMap);
	      
	      //请求成功
	      if(StringUtil.convertMapString(reMap, "status").equals("0")){


	      Map<String, Object> resultMap = (Map) ((List) (reMap.get("result"))).get(0);
	      Map<String, Object> disMap = (Map)resultMap.get("distance");
	      Map<String, Object> timeMap = (Map)resultMap.get("duration");
	      
	      String distanceStr = StringUtil.convertMapString(disMap, "text");
	      Double distanceValue = Double.parseDouble(StringUtil.convertMapString(disMap, "value"));
	      String timeStr = StringUtil.convertMapString(timeMap, "text");
	      Double time = Double.parseDouble(StringUtil.convertMapString(timeMap, "value"));
	      
	      map.put("DISTANCE_TEXT", distanceStr);
	      map.put("DISTANCE", distanceValue);
	      map.put("TIME_TEXT", timeStr);
	      map.put("TIME", time);
	      }
	return map;
	}


	 /**
	* @Title: loadJSON
	* @Description: 得到请求返回结果
	* @param url 百度API请求地址
	* @return 返回String雷锤
	*/
	public static String loadJSON (String url) {  
	          StringBuilder json = new StringBuilder();  
	          try {  
	              URL oracle = new URL(url);  
	              URLConnection yc = oracle.openConnection();  
	              BufferedReader in = new BufferedReader(new InputStreamReader(  
	                                          yc.getInputStream()));  
	              String inputLine = null;  
	              while ( (inputLine = in.readLine()) != null) {  
	                  json.append(inputLine);  
	              }  
	              in.close();  
	          } catch (MalformedURLException e) {  
	         e.printStackTrace();
	          } catch (IOException e) {  
	         e.printStackTrace();
	          }  
	          return json.toString();  
	      }  

	public static void main(String[] args) {
		
	//String point1 = "{'lng':117.14566177091,'lat':31.842170749583}";

	//String point2 = "{'lng':117.14555178002,'lat':31.84196792998}";
		
	String point1 = "{'lng':114.4291917726292,'lat':30.418032456341848}";
	String point2 = "{'lng':114.42473839917815,'lat':30.420710098164225}";

	Map map = BaiduAPI.getDistanceByPoints(point1, point2);


	System.out.println("distanceText:   " + map.get("DISTANCE_TEXT"));

	  System.out.println("value:   " + map.get("DISTANCE"));
	}
	
}
